package com.sptpc.demo1.controller;


import com.sptpc.demo1.controller.requests.ModifyDrinkRequest;
import com.sptpc.demo1.controller.requests.ModifyStudentRequest;
import com.sptpc.demo1.model.Drink;
import com.sptpc.demo1.model.Student;
import lombok.extern.slf4j.Slf4j;
import org.joda.money.Money;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@ResponseBody
@RequestMapping("/student")
public class StudentController {
    /*新增学生*/
    @PostMapping("/add")
    public Student addStudent(@RequestBody Student student){
        log.info("新增学生：{}",student);
        LocalDate localDate= LocalDate.of(20019,2,5);
        Student addstudent=Student.builder()
                .name("张三")
                .adminssionDate(localDate)
                .major("移动应用开发")
                .build();
        return addstudent;

    }
    //删除
    @DeleteMapping("/del/{id}")
    public String deleteStudent(Long id){
        log.info("删除的id为: {}",id);
        return "删除成功";
    }

    /*查询所有*/
    @GetMapping("/all")
    public List<Student> getAll(){
        LocalDate localDate= LocalDate.of(2018,2,5);
        List<Student> result = new ArrayList<>();
        result.add(Student.builder().id(1L)
                .name("张三")
                .adminssionDate(localDate)
                .major("移动开发")
                .build());
        LocalDate localDate1= LocalDate.of(2019,7,30);
        result.add(Student.builder().id(2L)
                .name("周六")
                .adminssionDate(localDate1)
                .major("移动开发")
                .build());
        return result;
    }

    @GetMapping("/{id}")
    public Student getDrink(@PathVariable Long id) {
        LocalDate localDate= LocalDate.of(20019,2,5);
        log.info("查询到的id: {}",id);
        return Student.builder().id(1L)
                .name("张三")
                .adminssionDate(localDate)
                .major("移动开发")
                .build();
    }

    @PutMapping("/updata")
    public Student modifDrink(@RequestBody ModifyStudentRequest request){
        LocalDate localDate= LocalDate.of(20019,2,5);
        log.info("修改的学生信息为：{}",request);
        Student updataStu= Student.builder()
                .id(1L)
                .name("张三")
                .adminssionDate(localDate)
                .major("移动开发")
                .build();
        return updataStu;
    }


    @PostMapping(value = "/bacth", consumes = {MediaType.MULTIPART_FORM_DATA_VALUE})
    public  List<Student> uploadFiles(MultipartFile file) {
        List<Student> result = new ArrayList<>();
        try {
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(file.getInputStream()));
            String line = "";
            while ((line = reader.readLine()) != null) {
                String[] args = line.split(",");
                Student student = Student.builder()
                        .id(Long.parseLong(args[0]))
                        .name(args[1])
                        .adminssionDate(LocalDate.parse(args[2]))
                        .major(args[3])
                        .build();
                result.add(student);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }


}

//@Controller
//@RequestMapping("/excel")
//public class ExcelDownloadController {
//
//    Gson mGson = new Gson();
//
//
//    @PostMapping(value = "/makeStatisticTableExcel")
//    private void makeStatisticTableExcel(@RequestParam(name = "sock_name") String data) throws IOException {
//        Map<String, Object> result = new HashMap<>();
//
//        TableToExcelEntity entity = mGson.fromJson(data, TableToExcelEntity.class);
//
//        //声明一个工作簿
//        HSSFWorkbook workbook = new HSSFWorkbook();
//        HSSFSheet sheet = workbook.createSheet("统计表");
//        sheet.setDefaultColumnWidth(20);
//        //声明表头
//        HSSFRow headrow = sheet.createRow(0);
//        HSSFCell tempCell = headrow.createCell(0);
//        HSSFRichTextString tempText = new HSSFRichTextString("");
//        tempCell.setCellValue(tempText);
//
//        for (int i = 0; i < entity.getHeader().size(); i++) {
//            String title = entity.getHeader().get(i);
//            //创建一个单元格
//            HSSFCell cell = headrow.createCell(i + 1);
//            //创建一个内容对象
//            HSSFRichTextString text = new HSSFRichTextString(title);
//            //将内容对象的文字内容写入到单元格中
//            cell.setCellValue(text);
//        }
//
//        for (int i = 0; i < entity.getTablelines().size(); i++) {
//            List<String> tableLine = entity.getTablelines().get(i);
//            if (tableLine == null || tableLine.isEmpty()) {
//                continue;
//            }
//            HSSFRow row = sheet.createRow(i + 1);
//            for (int j = 0; j < tableLine.size(); j++) {
//                String s = tableLine.get(j);
//                HSSFCell cell = row.createCell(j);
//                HSSFRichTextString text = new HSSFRichTextString(s);
//                cell.setCellValue(text);
//            }
//        }
//        //获取response
//        HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
//        //八进制输出流
//        response.setContentType("application/octet-stream");
//        //这后面可以设置导出Excel的名称，此例中名为statistic.xls
//        response.setHeader("Content-Disposition", "attachment;filename=statistic.xlsx");
//        //刷新缓冲
//        response.flushBuffer();
//        //workbook将Excel写入到response的输出流中，供页面下载
//        workbook.write(response.getOutputStream());
//    }
//}